package com.huisang.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huisang.springboot.entity.FileA;
import com.huisang.springboot.exception.ServiceException;
import com.huisang.springboot.mapper.FileMapper;
import com.huisang.springboot.service.FileService;
import com.huisang.springboot.utils.Code;
import com.huisang.springboot.utils.Result;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FileServiceImpl extends ServiceImpl<FileMapper, FileA> implements FileService {
    @Override
    public Result<IPage<FileA>> getFilePageList(Integer pageNum, Integer pageSize, String name) {
        IPage<FileA> page = new Page<>(pageNum, pageSize);
        QueryWrapper<FileA> queryWrapper = new QueryWrapper<>();
        Result<IPage<FileA>> result = new Result<>();
        try {
            queryWrapper.like(name!=null, "name", name);
            queryWrapper.eq("is_delete", false);
            queryWrapper.orderByDesc("id");
            IPage<FileA> fileIPage = page(page, queryWrapper);
            result.setCode(Code.success);
            result.setMessage("查询成功！");
            result.setData(fileIPage);
        } catch (Exception e) {
            result.setCode(Code.error);
            result.setMessage("查询失败！");
        } finally {
            return result;
        }
    }

    @Override
    public Result<Boolean> deleteFile(Integer id) {
        Result<Boolean> result = new Result<>();
        FileA byId = getById(id);
        byId.setIsDelete(true);
        boolean b = updateById(byId);
        if(b) {
            result.setCode(Code.success);
            result.setMessage("删除成功！");
            result.setData(b);
        }else {
            result.setCode(Code.error);
            result.setMessage("删除失败！");
            result.setData(b);
        }
        return result;
    }

    @Override
    public Result<Boolean> deleteBatchFile(List<Integer> ids) {
        Result<Boolean> result = new Result<>();
        QueryWrapper<FileA> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", ids);
        try {
            List<FileA> list = list(queryWrapper);
            for (FileA file : list) {
                file.setIsDelete(true);
                updateById(file);
            }
        } catch (Exception e) {
            throw new ServiceException(Code.service_error, "批量删除失败！");
        }
        result.setCode(Code.success);
        result.setMessage("批量删除成功！");
        result.setData(true);

        return result;
    }

    @Override
    public Result<Boolean> updateFile(FileA file) {
        Result<Boolean> result = new Result<>();
        boolean i = updateById(file);
        if(i) {
            result.setCode(Code.success);
            result.setMessage("修改成功！");
            result.setData(i);
        }else {
            result.setCode(Code.error);
            result.setMessage("修改失败！");
            result.setData(i);
        }
        return result;
    }
}
